// 日志记录

package logger

import (
	"log"
	"os"
	"path/filepath"
	"time"
)

var (
	errorLog *log.Logger
)

func Init(logDir string) error {
	if err := os.MkdirAll(logDir, 0755); err != nil {
		return err
	}

	logFile := filepath.Join(logDir, time.Now().Format("2006-01-02")+".log")
	file, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	if err != nil {
		return err
	}

	errorLog = log.New(file, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile)
	return nil
}

func LogError(err error) {
	if errorLog != nil {
		errorLog.Printf("%+v\n", err)
	}
}
